/**
 * Created by wxl on 2019/4/24.
 */
// 时间格式转换 yyyy-mm-dd hh:mm:ss
const formatTime = dateString =>{
  const date = new Date(dateString);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
};

const formatNumber = n => {
  n = n.toString();
  return n[1] ? n : '0' + n
};

// 获取富文本html内容中的第一张图片
const getFirstImageByContentHtml = contentHtml =>{
  let firstImage;
  let imgReg = /<img.*?(?:>|\/>)/gi;
  let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
  let arr = contentHtml.match(imgReg);  // arr 为包含所有img标签的数组
  if(!arr){// 该文本字段中无图片
    arr = [];
    return firstImage = ''
  }
  for (let i = 0; i < arr.length; i++) {
    let src = arr[i].match(srcReg);
    //获取图片地址
    firstImage = src[1]
    break;
  }
  return firstImage;
};


export default {
  formatTime: formatTime,
  getFirstImageByContentHtml:getFirstImageByContentHtml
}
